package com.liangzhong.proxy.techBar
{
	import com.adobe.serialization.json.JSON;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.commandconst.pub.CMD_Public;
	import com.liangzhong.commandconst.techBar.CMD_TechBar;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.techBar.TechBarServerDataParse;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.techBar.TechBarConst;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *科技馆 
	 * @author lake
	 * 
	 */
	public class TechBarProxy extends BaseProxy
	{
		public static const ProxyName :String = "TechBarProxy";
		
		public function TechBarProxy(proxyName:String=ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 */		
		override public function onSocketRead(mainCmdID:uint,
											  subCmdID:uint,
											  buffer:ByteArray, 
											  dataSize:int, 
											  clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Logic.MAIN:
				{
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
				}
				case CMD_Public.MAIN://公用协议
				{
					return onDealPub(mainCmdID,subCmdID,buffer,dataSize);
				}
			}
			return false;
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 */		
		private function onDealPub(mainCmdID:uint,
								   subCmdID:uint,
								   buffer:ByteArray, 
								   dataSize:int):Boolean
		{
			var resObj :Object;
			var retcode :int;
			switch(subCmdID)
			{
				case CMD_Public.SUB_EQUIP_COMBINE://装备合成
				{
					resObj = TechBarServerDataParse.readEquipCombine(buffer);
					this.sendNotification(TechBarConst.EQUIP_COMBINE_RESULT,resObj);
					return true;
				}
				case CMD_Public.SUB_MATERIAL_COMBINE://材料合成
				{
					resObj = TechBarServerDataParse.readMaterialCombine(buffer);
					this.sendNotification(TechBarConst.MATERIAL_COMBINE_RESULT,resObj);
					return true;
				}
				case CMD_Public.SUB_STAR_COMBINE://球员合成
				{
					resObj = TechBarServerDataParse.readPlayerCombine(buffer);
					this.sendNotification(TechBarConst.PLAYER_COMBINE_RESULT,resObj);
					return true;
				}
				case CMD_Public.SUB_CLOTH_COMBINE://球衣合成
				{
					resObj = TechBarServerDataParse.readClothCombine(buffer);
					this.sendNotification(TechBarConst.CLOTH_COMBINE_RESULT,resObj);
					return true;
				}
				case CMD_Public.SUB_PLAYER_REFRESH://球员洗练
				{
					resObj = TechBarServerDataParse.readPlayerRefresh(buffer);
					this.sendNotification(TechBarConst.PLAYER_REFRESH_RESULT,resObj);
					return true;
				}
				case CMD_Public.SUB_ROLE_CHANGE://角色转职
				{
					resObj = TechBarServerDataParse.readRoleChange(buffer);
					this.sendNotification(TechBarConst.ROLE_CHANGE_RESULT,resObj);
					return true;
				}
				case CMD_Public.SUB_ROLE_STRENGTHEN://角色强化
				{
					resObj = TechBarServerDataParse.readRoleRefresh(buffer);
					this.sendNotification(TechBarConst.ROLE_REFRESH_RESULT,resObj);
					return true;
				}
			}
			return false;
		}
		/**
		 *处理信息 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint, 
								buffer:ByteArray, 
								dataSize:int, 
								clientSocket:IClientSocket):Boolean
		{
			var resObj :Object;
			var retcode :int;
			switch(subCmdID)
			{
				case CMD_TechBar.SUB_GRADE_UP://升级
				{
					resObj = TechBarServerDataParse.readGradeUp(buffer);
					this.sendNotification(TechBarConst.GRADE_UP_RESULT,resObj);
					return true;
				}
			}
			return false;
		}
		/**
		 * 升级
		 */		
		public function gradeUp():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,CMD_TechBar.SUB_GRADE_UP,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *装备合成 
		 * @param param
		 * 
		 */		
		public function equipCombine(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 6,CMD_Public.MAIN,CMD_Public.SUB_EQUIP_COMBINE,0);
			sendBuf.writeShort(param.id1);
			sendBuf.writeShort(param.id2);
			sendBuf.writeShort(param.id3);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *材料合成 
		 * @param param
		 * 
		 */		
		public function materialCombine(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 12,CMD_Public.MAIN,CMD_Public.SUB_MATERIAL_COMBINE,0);
			sendBuf.writeShort(param.id1);
			sendBuf.writeShort(param.id2);
			sendBuf.writeShort(param.id3);
			sendBuf.writeShort(param.id4);
			sendBuf.writeShort(param.id5);
			sendBuf.writeShort(param.id6);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *球员合成 
		 * @param param
		 * 
		 */		
		public function playerCombine(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 12,CMD_Public.MAIN,CMD_Public.SUB_STAR_COMBINE,0);
			sendBuf.writeShort(param.id1);
			sendBuf.writeShort(param.id2);
			sendBuf.writeShort(param.id3);
			sendBuf.writeShort(param.id4);
			sendBuf.writeShort(param.id5);
			sendBuf.writeShort(param.id6);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *球衣合成 
		 * @param param
		 * 
		 */		
		public function clothCombine(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 6,CMD_Public.MAIN,CMD_Public.SUB_CLOTH_COMBINE,0);
			sendBuf.writeShort(param.id1);
			sendBuf.writeShort(param.id2);
			sendBuf.writeShort(param.id3);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *球员洗练
		 * @param param
		 * 
		 */		
		public function playerRefresh(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,CMD_Public.MAIN,CMD_Public.SUB_PLAYER_REFRESH,0);
			sendBuf.writeInt(param.id1);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *角色转职
		 * @param param
		 * 
		 */		
		public function roleChange(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,CMD_Public.MAIN,CMD_Public.SUB_ROLE_CHANGE,0);
			sendBuf.writeByte(param.type);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *角色洗练
		 * @param param
		 * 
		 */		
		public function roleRefresh(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 8,CMD_Public.MAIN,CMD_Public.SUB_ROLE_STRENGTHEN,0);
			sendBuf.writeShort(param.id1);
			sendBuf.writeShort(param.id2);
			sendBuf.writeShort(param.id3);
			sendBuf.writeShort(param.id4);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *请求科技馆信息 
		 * @param $obj
		 * 
		 */
		public function getInfo(obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.TECHBAR_GET_INFO;
			ProxyUtils.instance.webClient(getInfoRes,variables,false);
		}
		/**
		 *请求科技馆信息返回信息 
		 * @param $data
		 * 
		 */
		private function getInfoRes(data:Object):void
		{
			var str:String = String(data);
			var results:Object = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().techBarManager.parseBaseInfo(obj.research);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
	}
}